import Modal from './modal'

import vue from 'vue'

export default {
  name: 'Modal',
  config(type, { title, content, onOk, onCancel }) {
    const ModalClass = vue.extend(Modal)
    const instance = new ModalClass({
      propsData: { title, content },
      methods: {
        remove: function() {
          document.body.removeChild(this.$el)
        },
        close: function() {
          this.remove()
        },
        cancel: function() {
          this.remove()
          onCancel && onCancel()
        },
        ok: function() {
          this.remove()
          onOk && onOk()
        }
      }
    }).$mount()
    instance.type = type
    instance.$el.classList.add(`${type}-modal`)
    document.body.appendChild(instance.$el)
  },

  delete({ content = '确认删除吗？', onOk, onCancel }) {
    this.config('warning', { content, onOk, onCancel })
  },

  confirm({ content, onOk, onCancel }) {
    this.config('confirm', { content, onOk, onCancel })
  }
}
